// pages/share/share.js
const app = getApp()
const ERR_OK = 200
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShare: false,
    goodsImg: '',
    goodsId: '',
    shareImg: '',
    goodsName: '分享商品'
  },
  preventAction() {
    return
  },
  imgDownload() {
    
    wx.downloadFile({
      url: this.data.shareImg,
      success: function (res) {
        console.log(res);
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log("settingdata", settingdata)
                    },
                    fail(failData) {
                      console.log("failData", failData)
                    },
                    complete(finishData) {
                      console.log("finishData", finishData)
                    }
                  })
                }
              })
            }
          }
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  openShare() {
    let me = this
    wx.showLoading({
      title: '保存中',
    })
    wx.login({
      success: (res) => {
        wx.request({
          url: 'https://hpy.izhixiu.cn/api/front/appletPay/appletPay',
          data: {type: 'share', id: me.data.goodsId, code: res.code},
          method: 'GET',
          success: function(res) {
            console.log(res)
            if (res.data.code === ERR_OK) {
              wx.hideLoading()
              me.setData({
                shareImg: res.data.data.url
              })
            }
          }
        })

      },
    })
    this.setData({
      showShare: true
    })
  },
  closeShare() {
    this.setData({
      showShare: false
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (options.shareInfo) {
    //   let data = JSON.parse(options.shareInfo)
    //   this.setData({
    //     goodsId: data.goodsId,
    //     goodsImg: data.goodsImg,
    //     shareImg: data.shareImg,
    //     goodsName: data.name ? data.name : '分享商品'
    //   })
    // }
    let goodsInfo = JSON.parse(options.goodsInfo)
    this.setData({
      goodsImg: goodsInfo.img,
      goodsId: goodsInfo.id,
      goodsName: goodsInfo.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var path = '/pages/index/index?return_url=' + encodeURIComponent(`https://hpy.izhixiu.cn/front/#/item-detail/${this.data.goodsId}`);
    var title = this.data.goodsName
    return {
      title: title,
      path: path,
      imageUrl: this.data.goodsImg
    }
  }
})